<?php namespace Common\Model;
use Hdphp\Model\Model;
/**
 * 用户模型
 */
class User extends Model
{
	protected $table = "user";
	protected $validate = array(
	  array('mobile','phone','请输入正确手机号',3,3),
	  array('password','required','请输入密码',3,3),
	);
	/**
	 * 注册
	 */
	public function reg()
	{
		if(!$this->create()) return false;
		$password = md5($_POST['password']);
		if($password != md5($_POST['confirm']))
		{
			$this->error='两次密码不一致';
			return false;
		}
		$this->data['password']=$password;
		return $this->add();
	}
	/**
	 * 登录
	 */
	public function login()
	{
		if(!$this->create()) return false;
		//接收post的数据
		$mobile = $_POST['mobile'];
		$password = md5($_POST['password']);
		//验证手机号是否存在
		$data = $this->where('mobile',$mobile)->first();
		if(!$data){
			$this->error = "手机号不存在";
			return false;
		}
		//判断密码是否正确
		if($password != $data['password'])
		{
			$this->error = "密码错误";
			return false;
		}
		//如果走到这步说明登录成功
		$_SESSION['name'] = $data['name'];
		$_SESSION['id'] = $data['uid'];
		return true;
	}
}
